package com.example.developerlab;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceRequest;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;

public class WebViewActivity extends Activity {
    private static final String TAG = WebViewActivity.class.getSimpleName();
    private static final int PROCESS_BAR_MAX = 100;
    private WebView mWebView;
    private ProgressBar progressBar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_web_view);
        mWebView = findViewById(R.id.web_view);

        // 设置支持js否则有些网页无法打开
        mWebView.getSettings().setJavaScriptEnabled(true);
        mWebView.setWebViewClient(new MyClient());
        mWebView.setWebChromeClient(new MyWebChromeClient());
        // 加载网络url
        mWebView.loadUrl("https://www.baidu.com/");

        progressBar = findViewById(R.id.progress_bar);
        progressBar.setVisibility(View.VISIBLE);
    }

    class MyClient extends WebViewClient {
        // 监听到页面发生跳转的情况，默认打开web浏览器
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
            // 该处可以对H5调用scheme进行拦截，进而完成APP对外部跳转的管控
            String url = allowedScheme(request.getUrl().toString());
            if (url != null) {
                Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
                startActivity(intent);
                return true;
            }
            return super.shouldOverrideUrlLoading(view, request);
        }

        private String allowedScheme(String url) {
            if (url.startsWith("market")) {
                return url;
            }
            return null;
        }

        // 页面开始加载
        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            super.onPageStarted(view, url, favicon);
            Log.e(TAG, "onPageStarted:");
        }

        // 页面加载完成的回调方法
        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            Log.e(TAG, "onPageFinished:");
            // 加载完成后跳转应用市场微信
            // mWebView.loadUrl("javascript:window.location.href =\"market://details?id=com.tencent.mm\"");
        }
    }

    class MyWebChromeClient extends WebChromeClient {
        // 监听网页进度 newProgress进度值在0-100
        @Override
        public void onProgressChanged(WebView view, int newProgress) {
            super.onProgressChanged(view, newProgress);
            Log.d(TAG, "newProgress:" + newProgress);
            // 进行进度条更新
            if (newProgress == PROCESS_BAR_MAX) {
                progressBar.setVisibility(View.GONE);
            }
            progressBar.setProgress(newProgress);
            // 如果想展示加载动画，则增加一个drawable布局后，在onCreate时展示，在progress=100时View.GONE即可
        }
    }
}
